package com.lanyam.travel.base.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @author KingTang
 * @date   2019/12/20
 */
@Aspect
@Component
public class PerformanceMonitoring {

    @Pointcut("@annotation(com.lanyam.travel.base.annotation.PerformanceMonitoringCut)")
    public void performanceMonitoringCut(){
        //do nothing
    }

    @Around(value = "performanceMonitoringCut()")
    private Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        long end = System.currentTimeMillis();
        long timeDefine = 3000L;
        if (end - start > timeDefine){
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            String name = signature.getMethod().getName();
            System.out.println("=============================");
            System.out.println(name + ":    " + (end - start));
            System.out.println("=============================");
        }

        return proceed;
    }

}
